
namespace gen {

	///////////////////////////////////////////////////////////////////////////
    //
    //  limit class implementation
    //
    ///////////////////////////////////////////////////////////////////////////

	template <class DerivedT, class ContextT>
	inline DerivedT 
	limit_base<DerivedT,ContextT>::operator+(typename limit_base<DerivedT,ContextT>::diff_t offset)
	{
		DerivedT copy(this->m_base_cur, this->m_offset+offset);
		return copy;
	}

	template <class DerivedT, class ContextT>
	inline DerivedT 
	limit_base<DerivedT,ContextT>::operator-(typename limit_base<DerivedT,ContextT>::diff_t offset)
	{
		return operator+(-offset);
	}

	template <class DerivedT, class ContextT>
	inline DerivedT 
	operator+(typename limit_base<DerivedT,ContextT>::diff_t offset, limit_base<DerivedT,ContextT> const& l)
	{
		return l+offset;
	}

	template <class ContextT>
	typename next_context_limit<ContextT>::iterator_t 
	next_context_limit<ContextT>::get_limit(typename next_context_limit<ContextT>::parent_t const& p) const
	{
		typename ContextT::range_t		cont(*(p.current()));
		typename ContextT::iterator_t	i;
		if (limit_base_t::m_base_cur == limit_base_t::beg) {
			i = cont.begin();
		} else {
			i = cont.end();
		}
		return i+limit_base_t::m_offset;
	}

	///////////////////////////////////////////////////////////////////////////
    //
    //  position class implementation
    //
    ///////////////////////////////////////////////////////////////////////////

	template <typename S, typename LimitT>
	template <typename ContextT>
	void
	position<S,LimitT>::generate(ContextT & info) const
    {
		typedef typename LimitT::iterator_t		iterator_t;
		typedef typename LimitT::context_t		context_t;
		
		LimitT l_end(LimitT::end);
		iterator_t first = this->right().get_limit(info);
		iterator_t last = l_end.get_limit(info);
		context_t c(first, last, info);
        
		if (first!=last) this->left().generate(c);
		//info.reset(c.str());
    }

    template <typename DerivedT>
	template <class T, class ContextT>
	inline position< DerivedT, T >
	generator<DerivedT>::operator[](limit_base<T,ContextT> const& l) const
    {
        return position<DerivedT, T>(derived(), l.derived());
    }

	///////////////////////////////////////////////////////////////////////////
    //
    //  limit classes specialization
    //
    ///////////////////////////////////////////////////////////////////////////

	template <class ContextT>
	typename ContextT::iterator_t 
	context_limit<ContextT>::get_limit(ContextT const& p) const
	{
		typename ContextT::iterator_t i;
		if (limit_base_t::m_base_cur == limit_base_t::beg) {
			i = p.domain().first();
		} else {
			i = p.domain().last();
		}
		return i+limit_base_t::m_offset;
	}


}
